package controllers

import (
	"GO_BACK/api/dbapi"
	"GO_BACK/model/data"
	"fmt"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"net/http"
)

func CreateRecordController(c *gin.Context) {
	var users []*data.PostgresUserInfo
	err := c.BindJSON(&users)
	fmt.Println("users:", users)
	if err != nil {
		fmt.Println("err:", err)
	}
	var results []*gorm.DB
	dbconfig, _ := dbapi.PgDbConnect()
	for i := 0; i < len(users); i++ {
		user := users[i]
		result := dbapi.PgDbCreate(user, dbconfig)
		results[i] = result
	}
	for i := 0; i < len(results); i++ {
		if results[i].Error != nil {
			c.JSON(http.StatusBadRequest, gin.H{
				"message": "失败",
				"info":    results[i].Error,
			})
			break
		}
	}
	c.JSON(http.StatusOK, gin.H{
		"message": "失败",
		"info":    users,
	})
}
